package com.caigou.modules.center.ui.detail

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.utils.ImageLoader
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.center.R
import com.caigou.modules.center.adapter.MyScoreAdapter
import com.caigou.modules.center.bean.MyScoreDetailBean
import com.caigou.modules.center.bean.MyScoreItem
import com.caigou.modules.center.databinding.FragmentMyScoreBinding
import com.caigou.modules.center.viewmodel.DetailViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/11/08
 */
@AndroidEntryPoint
class MyScoreFragment : BaseFragment() {

    private var _binding: FragmentMyScoreBinding? = null
    private val binding get() = _binding!!

    private val detailViewModel: DetailViewModel by viewModels()

    private val list = mutableListOf<MyScoreItem>()

    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity(), RecyclerView.HORIZONTAL, false)
    }

    private val mAdapter: MyScoreAdapter by lazy {
        MyScoreAdapter(mutableListOf())
    }

    private val recyclerViewItemDecoration by lazy {
        SpaceItemDecoration(requireActivity())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentMyScoreBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.myScoreSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                detailViewModel.getMyScoreDetail(AppBackgroundInfo.instance.userBean!!.userId)
            }
        }

        binding.myScoreRecyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(recyclerViewItemDecoration)
        }

        binding.myScoreBtnAbout.setOnClickListener {
            (requireActivity() as DetailActivity).switchFragment(RouterPath.Center.TAG_ABOUT_SCORE)
        }

        binding.myScoreBtnDetail.setOnClickListener {
            (requireActivity() as DetailActivity).switchFragment(RouterPath.Center.TAG_SCORE_DETAIL)
        }

        binding.myScoreTvBack.setOnClickListener {
            (requireActivity() as DetailActivity).onBackPressed()
        }
    }

    override fun lazyLoad() {
        detailViewModel.getMyScoreDetail(AppBackgroundInfo.instance.userBean!!.userId)
        list.add(MyScoreItem(R.drawable.img_center_lock1, "88-188积分"))
        list.add(MyScoreItem(R.drawable.img_center_lock2, "189-360积分"))
        list.add(MyScoreItem(R.drawable.img_center_lock3, "361-888积分"))
        list.add(MyScoreItem(R.drawable.img_center_lock4, "889-1888积分"))
        list.add(MyScoreItem(R.drawable.img_center_lock5, "1889-9999积分"))
        mAdapter.setList(list)
    }

    override fun onBack() {
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(detailViewModel.myScoreDetailLiveData, ::handleGetMyScoreDetail)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun handleGetMyScoreDetail(result: Resource<MyScoreDetailBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.myScoreSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取积分信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.myScoreSwipeRefreshLayout.isRefreshing = false
                if (result.data == null) {
                    return
                }

                if (TextUtils.isEmpty(result.data!!.headImg)) {
                    binding.myScoreIvAvatar.setImageResource(R.drawable.img_comm_avatar)
                } else {
                    ImageLoader.loadCircle(requireActivity(), result.data!!.headImg, binding.myScoreIvAvatar)
                }

                if (TextUtils.isEmpty(result.data!!.growthIcon)) {
                    binding.myScoreIvLv.setImageResource(R.drawable.img_comm_lv1)
                } else {
                    ImageLoader.load(requireActivity(), result.data!!.growthIcon, binding.myScoreIvLv)
                }

                binding.myScoreTvNickname.text = result.data!!.nickName
                binding.myScoreTvNum.text = result.data!!.score.toString()
                binding.myScoreTvDay.text = result.data!!.registerDays.toString()
            }
        }
    }
}